package com.doctor.windflower_doctor.activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.actionBeen.QuestionAction;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.util.LogUtils;
import com.doctor.windflower_doctor.util.StringUtil;
import com.doctor.windflower_doctor.view.LoadingDialog;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * <p>
 * 功能 问题反馈
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年10月29日 下午5:22:26
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class FeedBackActivity extends BaseActivity implements OnClickListener, Constent {
	private EditText feedback_edit;
	private TextView textView;
	private ImageButton back_btn;
	private TextView tv_save;
	private LoadingDialog loadingDialog;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {

		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {

		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		back_btn.setOnClickListener(this);
		tv_save.setOnClickListener(this);
	}

	@Override
	protected void findViews() {
		loadingDialog = new LoadingDialog(this);
		textView = (TextView) findViewById(R.id.textView);
		tv_save = (TextView) findViewById(R.id.tv_save);
		tv_save.setVisibility(View.VISIBLE);
		textView.setText("问题反馈");
		back_btn = (ImageButton) findViewById(R.id.back_btn);
		feedback_edit = (EditText) findViewById(R.id.feedback_edit);
		tv_save.setText("提交");

	}

	@Override
	public int getResLayoutId() {
		return R.layout.activity_feedback_problem;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;
		case R.id.tv_save:
			if (StringUtil.isEmpty(feedback_edit.getText().toString().trim())) {
				Toast.makeText(FeedBackActivity.this, "反馈内容不能为空", Toast.LENGTH_SHORT).show();
			} else {
				post();
			}
			break;
		}
	}

	/**
	 * <p>
	 * 功能 提交意见反馈
	 * </p>
	 * 
	 * @author 皮勉 时间 2014年11月21日 下午3:29:05
	 */
	void post() {
		// 获取packagemanager的实例
		PackageManager packageManager = getPackageManager();
		// getPackageName()是你当前类的包名，0代表是获取版本信息
		PackageInfo packInfo = null;
		try {
			packInfo = packageManager.getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e1) {
			e1.printStackTrace();
		}
		String version = packInfo.versionName;
		HttpMethod requestMethod = HttpMethod.POST;
		String requestUrl = URL_1 + API_FEEDBACKS_SUBMIT;
		LogUtils.e("requestUrl>>>>>>>>>" + requestUrl);
		HttpRequestCallBack<QuestionAction> requestCallBack = new HttpRequestCallBack<QuestionAction>(new JsonParser(), QuestionAction.class) {
			@Override
			protected void handleResult(HttpResponseInfo<QuestionAction> responseInfo) {
				loadingDialog.close();
				LogUtils.e("=====API_FEEDBACKS_SUBMIT=======" + responseInfo.success.success);
				if (responseInfo.success.success) {
					LogUtils.e("=====API_FEEDBACKS_SUBMIT====成功===");
					feedback_edit.setText("");
					finish();
				}
			}

			@Override
			protected void handleError(HttpException e, String msg) {
				loadingDialog.close();
				LogUtils.e("=====getUserInfermation====3===" + msg);
			}

			@Override
			public void onStart() {
				loadingDialog.show();
				super.onStart();

			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod, requestUrl, requestCallBack);
		params.addBodyParameter(APPSECRET, "123123");
		params.addBodyParameter(CONTENT, feedback_edit.getText().toString().trim());
		params.addBodyParameter(SYSTEMVERSION, android.os.Build.VERSION.SDK_INT + "");
		params.addBodyParameter(DEVICENUMBER, Build.MODEL);
		params.addBodyParameter(FXZDOCTORVERSION, version);
		params.addBodyParameter(SUBMITTER, SharedPreferencesDB.getInstance(this).getUserName());
		HttpRequest.send(params);
	}
}
